package P150;

/**
 * @author LiangMu
 * @version \$Id: P42.java, v 0.1, 2024年07月06日 02:07 LiangMu Exp $$
 */
public class P42 {

    public boolean isHappy(int n) {
        int slow = getNext(n);
        int fast = getNext(getNext(n));
        while (slow != fast && fast != 1) {
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }
        return slow != fast;
    }


    public int getNext(int n) {
        int sum = 0;
        while (n > 0) {
            int i = n % 10;
            n -= (n - i) / 10;
            sum += i * i;
        }
        return sum;
    }

}
